package com.example.fwx;

import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;



public class RegisterActivity extends AppCompatActivity {

    private Button btn_Register;
    private EditText et_UserName, et_Password, et_Password2, et_Phone;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        setTitle("注册");
        //用户名及密码
        et_UserName = this.findViewById(R.id.zc_account);
        et_Password = this.findViewById(R.id.zc_pwd);
        et_Password2 = this.findViewById(R.id.zc_pwdCopy);
        //手机号
        et_Phone = this.findViewById(R.id.zc_phone);

        //确认注册按钮
        btn_Register = this.findViewById(R.id.btn_register);


        //确认注册处理
        btn_Register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取用户输入信息
                String username = et_UserName.getText().toString();
                String password = et_Password.getText().toString();
                String password2 = et_Password2.getText().toString();
                String phone = et_Phone.getText().toString();
                //判断用户输入是否为空，若有一个输入框为空，则给出提示信息

                if (username.equals("") || password.equals("") || password2.equals("") || phone.equals("")) {
                    Toast.makeText(RegisterActivity.this, "请输入完整信息", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!password.equals(password2)) {

                    Toast.makeText(RegisterActivity.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                    //若允许注册，则将用户输入的信息，插入到数据库表（user）中,插入成功跳转到登录界面
                } else if (!(phone.length() == 11)) {
                    Toast.makeText(RegisterActivity.this, "请输入11位手机号！", Toast.LENGTH_SHORT).show();
                    return;
                }
                DBHelper helper = new DBHelper(RegisterActivity.this);
                //查询账号密码，若用户名存在，提示用户更换
                Cursor query = helper.getReadableDatabase().query(DBHelper.TB_USER, null, "name=?", new String[]{username}, null, null, null);
                if (query.getCount() > 0) {
                    Toast.makeText(RegisterActivity.this, "用户名已存在", Toast.LENGTH_SHORT).show();
                } else {
                    ContentValues cv = new ContentValues();
                    cv.put("name", username);
                    cv.put("pwd", password);
                    long res = helper.getWritableDatabase().insert(DBHelper.TB_USER, null, cv);
                    if (res >= 0) {
                        Toast.makeText(RegisterActivity.this, "注册成功,请登录！", Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        Toast.makeText(RegisterActivity.this, "注册失败！", Toast.LENGTH_SHORT).show();
                    }

                }


            }
        });
    }

}